package server;

import java.util.Collection;
import java.util.Iterator;
import java.util.Scanner;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import communication.Message;
import communication.exception.AuthenticationException;
import communication.exception.NullResultExeption;

import server.command.AnalysisCommand;
import server.core.base.User;
import server.core.dao.DaoFactory.daoType;
import server.core.dao.DaoLoad;
import server.core.util.Authentication;

public class CLI extends DaoLoad implements Runnable{
	protected static final Logger LOCAL_LOG=LogManager.getLogger(LogManager.ROOT_LOGGER_NAME);
	public CLI(daoType type, String[] strings) {
		// TODO Auto-generated constructor stub
		this.args=strings;
		this.typeOfDao=type;
		loadDao(typeOfDao);
		this.in=new Scanner(System.in);

	}

	public void printCol(Collection<?> collection) throws NullResultExeption {
		Iterator<?> itr = collection.iterator();
		while (itr.hasNext()) 
			System.out.println(itr.next());
	}
	public String stringCol(Collection<?> collection) throws NullResultExeption {
		Iterator<?> itr = collection.iterator();
		StringBuilder stringBuilder=new StringBuilder(65536);
		while (itr.hasNext()){
			stringBuilder.append(itr.next().toString());
			stringBuilder.append("\n");
		}
		return stringBuilder.toString();
	}
	private User currentUser;
	private String inputed;
	private Scanner in;
	private daoType typeOfDao;
	private String[] args;
	
	public User getCurrentUser(){
		return currentUser;
	}
	
	public void setCurrentUser(User currentUser) {
		this.currentUser = currentUser;
	}

	public void inCheck(){
		String name;
		String password;
		
		while(true){
			while(getCurrentUser()==null){
				LOCAL_LOG.info("Authentication: waiting");
				System.out.println("Input name and password");
				name=in.next();
				password=in.next();
				LOCAL_LOG.info("Authentication: process");
				try {
					this.setCurrentUser(Authentication.getInstance().signup(name, password));
				} catch (AuthenticationException e) {
					e.printStackTrace();
				}
			}
			System.out.println("Input \"<command>\", \"help\", \"logout\" or \"exit\".");
			inputed=in.next();
			inputed+=in.nextLine();
			inputed.toUpperCase();
			LOCAL_LOG.info("Inputed <"+inputed+">");
			if("EXIT".equalsIgnoreCase(inputed))
				break;
			else if("LOGOUT".equalsIgnoreCase(inputed)){
				setCurrentUser(null);
				LOCAL_LOG.info("Logout: done");
				continue;
			}
			else{
				LOCAL_LOG.info("AnalysingCommand: start");
				try{
					AnalysisCommand command=new AnalysisCommand(inputed, getCurrentUser(), typeOfDao);
					printCol(command.getResult());
				}catch(Exception e){
					System.out.println(e);
				}
			}
		}
		LOCAL_LOG.info("Exit");
		in.close();
		System.exit(0);
	}
	
	public Message checkStr(Message request){
		Message message=new Message();
				try{
					AnalysisCommand command=new AnalysisCommand(request.getCommand(), getCurrentUser(), typeOfDao);
					message.setCommand(stringCol(command.getResult()));
				}catch(Exception e){
					message.setException(e);
				}
				return message;
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
//		Network network=new Network(args, System.out);
//		new Thread(network).start();
	}
}